package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.constant.OrderStatusConstants;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.view.casherdesk.CasherDeskView;
import com.superman.supermarket.client.view.casherdesk.CasherDeskView.CasherDeskViewPresenter;

/**
 * 收银台
 * 
 * @author superman
 * @version 2018年8月2日下午5:08:48
 */
public class CasherDeskActivity extends ShopAbstractActivity implements CasherDeskViewPresenter {

	@Inject
	CasherDeskView casherDeskView;
	private String orderid;

	private void dispath() {
		orderid = ActivityJumpUrl.getPayNotifyOrderId();
		getOrderDetail();
	}

	private void getOrderDetail() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderid);
		postman.post("order/query2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				parseOrderDetail(XJsonUtil.getBody(response));
			}
		});
	}

	@Override
	public IPageView getView() {
		return casherDeskView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
	}

	private void parseOrderDetail(Map<String, Object> map) {
		int orderState = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
		double payPrice = Double.parseDouble(String.valueOf(map.get("PAY_FEE")));
		casherDeskView.setPayPrice(payPrice);
		if (orderState == OrderStatusConstants.TO_PAY.getStatus()) {
			// 等待支付支付,点击的时候 前往支付
			casherDeskView.setSubmitBtn(true);
		} else {
			// 别的状态,按钮点击是查看订单详情
			casherDeskView.setSubmitBtn(false);
			casherDeskView.setPayInfo(map);
		}
	}

	@Override
	public void pay() {
		String returnUrl = Window.Location.getProtocol() + Window.Location.getHost() + Window.Location.getPath();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderid);
		map.put("PAYMENT", casherDeskView.getPaymentType());
		map.put("RETURN_URL", returnUrl);
		postman.post("order/payment2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				XGinjector.INSTANCE.getUserModel().saveToCache();

				Map<String, Object> map = XJsonUtil.getBody(response);
				String payUrl = String.valueOf(map.get("PAYMENT_STR"));

				Window.Location.assign(payUrl);
			}
		});
	}

	@Override
	public void popToRefresh() {
		dispath();
	}

	@Override
	public void popToRefresh(Object contextData) {
		dispath();
	}

	@Override
	public void pushToOrderDetail() {
		ActivityJumpUrl.pushToOrderDetailPage(orderid);
	}

	@Override
	public void pushToRefresh() {
		dispath();
	}

}
